package com.lonely.fx.property;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @date: 2022-07-21 17:58
 * @author: ztkj-hzb
 * @description: 针对 List类型的 监听包装
 **/
public class ListPropertySettingMain {

    public static void main(String[] args) {


        SimpleListProperty<String> listProperty = new SimpleListProperty<>();
        ObservableList<String> observableList = FXCollections.observableArrayList("a", "b");
        listProperty.set(observableList);

        // ChangeListener
        /*listProperty.addListener(new ChangeListener<ObservableList<String>>() {
            @Override
            public void changed(ObservableValue<? extends ObservableList<String>> observable, ObservableList<String> oldValue, ObservableList<String> newValue) {
                //注意，在这里，observable 和 oldValue 以及 newValue 是同一个对象
                System.out.println(newValue.hashCode());
                System.out.println(oldValue.hashCode());
            }
        });*/


        // 列表内容变化监听
        listProperty.addListener(new ListChangeListener<String>() {
            @Override
            public void onChanged(Change<? extends String> c) {

                System.out.println("进入 onChanged 事件");

                //获取此时list数据
                ObservableList<? extends String> list = c.getList();

                //System.out.println(c.wasAdded());

                //TODO 注意，想是list操作的，需要先执行 next()方法，才可以进行下面的判断和操作,否则会报错
                if (c.next()) {
                    System.out.println("是否是添加操作：" + c.wasAdded());
                    System.out.println("是否是移除操作：" + c.wasRemoved());
                    System.out.println("是否是替换操作：" + c.wasReplaced());
                    System.out.println("是否是修改操作：" + c.wasUpdated());

                    //list变化中，添加的元素个数/移除的个数
                    System.out.println("本次添加的元素个数" + c.getAddedSize());
                    System.out.println("本次移除的元素个数" + c.getRemovedSize());

                    System.out.println("影响开始位置：" + c.getFrom());
                    System.out.println("影响结束位置：" + c.getTo());

                    //本次添加的元素集合
                    List<? extends String> addedSubList = c.getAddedSubList();
                    System.out.println("本次添加的元素：" + addedSubList.stream().collect(Collectors.joining(",")));

                    //本次删除的元素集合
                    List<? extends String> removed = c.getRemoved();
                    System.out.println("本次删除的元素：" + removed.stream().collect(Collectors.joining(",")));

                }

                System.out.println("\r\n");

            }
        });

        listProperty.addAll("fdafda", "fdafdsa");
        //listProperty.add(0, "hello");
        listProperty.remove(0);
        listProperty.set(0, "aaaa");

    }
}
